<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\GuildUserIncome;
class GuildUserCommand extends Command
{
    /**
     * The name and signature of the console command.
     *	//php artisan guilduser income
     * @var string
     */
    protected $signature = 'guilduser {param : income|comm}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计公会每日收益';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
		$param = $this->argument('param');
		if($param == 'income') {
			$this->insertIncome();
		} elseif($param == 'comm') {
			$this->comm();
		}
    }
	/**
	 * 公会每天收益统计入库
	 * 每天凌晨5点执行一次
	 */
	public function insertIncome(){
		//获取公会
		$guild = (new \App\Service\ApiService())->getGuildShow();
		if(empty($guild)) {
			exit('公会数据为空'."\n");
		}
		$day = date('Y-m-d', strtotime("-1 day"));
		foreach($guild as $v) {
			//接口每天数据
			$rest = (new \App\Service\ApiService())->getEarning(['guild' => $v['id'], 'start_time' => $day]);
			//判读收益是否已经存在
			$income = GuildUserIncome::where(['gid' => $v['id'], 'day' => $day])->first();
			if(!empty($rest['list'][0])) {
				$data = $rest['list'][0];
				if(!empty($income)) {
					$income->sum_gift = $data['sum_gift'];
					$income->sum_stamp = $data['sum_stamp'];
					$income->sum_live = $data['sum_live'];
					$income->sum_voice = $data['sum_voice'];
					$income->cale = $data['cale'];
					$income->sum_guild = $data['sum_guild'];
					$income->updated_at = date('Y-m-d H:i:s');
					$income->save();
				}
				else {
					$modesOne = new GuildUserIncome();
					$modesOne->gid = $data['guild'];
					$modesOne->title = $data['title'];
					$modesOne->day = $data['date'];
					$modesOne->sum_gift = $data['sum_gift'];
					$modesOne->sum_stamp = $data['sum_stamp'];
					$modesOne->sum_live = $data['sum_live'];
					$modesOne->sum_voice = $data['sum_voice'];
					$modesOne->cale = $data['cale'];
					$modesOne->sum_guild = $data['sum_guild'];
					$modesOne->updated_at = date('Y-m-d H:i:s');
					$modesOne->save(); 
				}				
			}
			else {				
				if(empty($income)) {
					$model = new GuildUserIncome();
					$model->gid = $v['id'];
					$model->title = $v['username'];
					$model->day = $day;
					$model->save();
				}
			}
		}
		exit('执行完成');
	}
	/**
	 * 统计公会每天收益入账
	 * 每天凌晨6执行一次
	 */
	public function comm(){
		$income = GuildUserIncome::where('is_task', 0)->get()->toArray();
		if(empty($income)) {
			exit('暂无数据');
		}
		foreach($income as $v) {
			$comm = \App\Models\GuildUserComm::where('gid', $v['gid'])->first();
			if(!empty($comm)) {
				$comm->balance += $v['sum_guild'];
				$comm->total_money += $v['sum_guild'];
				$comm->save();
			}
			$getOne = GuildUserIncome::where(['id' => $v['id']])->first();
			$getOne->is_task = 1;
			$getOne->task_time = date('Y-m-d H:i:s');
			$getOne->save();
		}
		exit('执行完成');
	}
}